// http://poj.org/problem?id=1915
#include <iostream>
#include <queue>
#include <cstring>
#include <cstdio>
using namespace std;


const int MAX_SIZE = 305;
bool visited[MAX_SIZE][MAX_SIZE];
int dx[] = {-2, -1, 1, 2, 2, 1, -1, -2};
int dy[] = {1, 2, 2, 1, -1, -2, -2, -1};
int N = 8;

int bfs(int boardSize, int x, int y, int targetX, int targetY) {
    if (x == targetX && y == targetY) {
        return 0;
    }
    memset(visited, false, sizeof visited);
    visited[x][y] = true;
    queue<pair<int, int> > Q;
    Q.push(make_pair(x, y));
    int count = 0;
    while (!Q.empty()) {
        int qsize = Q.size();
        for (int w=0; w<qsize; w++) {
            const int cx = Q.front().first, cy = Q.front().second;
            for (int i = 0; i < N; i++) {
                int nx = cx + dx[i], ny = cy + dy[i];
                if (nx == targetX && ny == targetY) {
                    return count + 1;
                }
                if (nx < boardSize && nx >= 0 && ny < boardSize && ny >= 0 && !visited[nx][ny]) {
                    Q.push(make_pair(nx, ny));
                    visited[nx][ny] = true;
                }
            }
            Q.pop();
        }
        count++;
    }
    return 0;
}

int main() {
    int m, t, x, y, targetX, targetY;
    scanf("%d", &m);
    for (int i=0; i<m; i++) {
        scanf("%d%d%d%d%d", &t, &x, &y, &targetX, &targetY);
        printf("%d\n", bfs(t, x, y, targetX, targetY));
    }
    return 0;
}
